/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.cloudlayouter;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import twaver.Element;
import twaver.ResizableNode;
import demo.DemoNode;
import demo.DemoPane;

public class DemoImageNode extends ResizableNode{
	private boolean demoLoaded=false;
	private Image demoImage=null;
	private DemoNode demoNode;
	private int maxImageSize=200;
	private int imageWidth=maxImageSize;
	private int imageHeight=maxImageSize;
	
	/**
	 * 1 - Network Demo
	 * 2 - Chart Demo
	 * 3 - Other Component Demo
	 * 4 - Other Demo
	 */		
	private int category=0;
	
	public DemoImageNode(DemoNode node){
		super();
		setDemoNode(node);
	}
	
	public DemoNode getDemoNode() {
		return demoNode;
	}
	
	public void setDemoNode(DemoNode demoNode) {
		this.demoNode = demoNode;
		
		Element tempParent = demoNode.getParent();
		while(tempParent.getParent()!=null){
			tempParent=tempParent.getParent();
		}
		if(tempParent!=null){
			String categoryName = tempParent.getName();
			if("Network".equals(categoryName)){
				category=1;
			}else if("Chart".equals(categoryName)){
				category=2;
			}else if("Tree".equals(categoryName)||"Sheet".equals(categoryName)||"Table".equals(categoryName)||"List".equals(categoryName)){
				category=3;
			}else{
				category=4;
			}
		}
		resetImage();
	}

	public void resetImage() {
		DemoPane demoPane = demoNode.getDemoPane();
		int width = 0;
		int height = 0;
		if (demoPane != null) {
			width = demoPane.getWidth();
			height = demoPane.getHeight();
			if (width > 0 && height > 0) {
				double scale = (double)maxImageSize / width;
				width *= scale;
				height *= scale;
				BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
				Graphics2D g2 = (Graphics2D) image.getGraphics();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				g2.scale(scale, scale);
				demoPane.paint(g2);
				this.setDemoLoaded(true);
				this.setDemoImage(image);
				imageWidth=width;
				imageHeight=height;
			}
		}
		updateUI();
	}
	
	
	public int getImageWidth() {
		return imageWidth;
	}

	public int getImageHeight() {
		return imageHeight;
	}

	public String getUIClassID() {
		return DemoImageNodeUI.class.getName();
	}
	
	public boolean isDemoLoaded() {
		return demoLoaded;
	}

	public void setDemoLoaded(boolean demoLoaded) {
		this.demoLoaded = demoLoaded;
	}
	public void setDemoImage(Image demoImage){
		this.demoImage=demoImage;
	}
	public Image getDemoImage() {
		return demoImage;
	}
	
	public boolean isLabelVisible() {
		return isSelected()||super.isLabelVisible();
	}

	public int getCategory() {
		return category;
	}
}